# -*- coding: utf-8 -*-

from common.lottery import LOTTERY_TYPE
from common.lottery.cyclical.abstract.activity import calc_trend_stats
from common.lottery.cyclical.sh_11x5.db import activity as db


def get_trend(last_n, idx):
    datas = [[]] * 5
    infos = []
    items = db.get_activity_stats(last_n)
    if idx is not None:
        idx_list = ['idx_%s' % idx]
    else:
        idx_list = ['idx_%s' % x for x in range(5)]

    for item in items:
        info = {
            'term': item.term,
            'number': item.number,
            'repeat_numbers': item.repeat_numbers
        }
        for i, idx in enumerate(idx_list):
            idx_data = getattr(item, idx).split(',')
            idx_data = [int(x) for x in idx_data]
            datas[i].append(idx_data)
            info[idx] = idx_data
        infos.append(info)
    stats = {}
    for i, idx in enumerate(idx_list):
        stats[idx] = calc_trend_stats(LOTTERY_TYPE.SH_11X5, datas[i])

    return {
        'list': infos,
        'stats': stats
    }
